/* Implementation of list data structure of python in c using arrays */

#include<stdio.h>
#include<malloc.h>

int *a=NULL, n=0;

void append(int val)
{
if (a==NULL) 
{
a =(int*) malloc(sizeof(int));
a[0]=val;
n++;
}

else
{
	a=(int*)realloc(a,(n+1)*(sizeof(int)));
	a[n]=val;
	n++;
}

}

void print()
{
printf("\nThe current list is ");
for (int i=0;i<n;i++) printf("%d ",a[i]);
}

void merge()
{
int x,y;
printf("\nEnter the number of elements of the 2nd array: ");
scanf("%d",&x);
a=realloc(a,(n+x)*sizeof(int));
int i;
for (i=n;i<(n+x);i++)
{
	printf("\nEnter the %d element: ",i);
	scanf("%d",&y);
	a[i]=y;
}
n+=x;
}
	


void insert(int val, int pos)
{
int i=n-1;
a=realloc(a,(n+1)*sizeof(int));
while (i>pos)
{
	a[i+1]=a[i];
	i--;
}
a[i]=val;
n++;
}





int index1(int m)
{int i=0;
    for ( i=0;i<n;i++)
	{ if (a[i]==m) return i;
	}
    if(i==n) return -1;
}

int pop(int m)
{int i=0,j=0;

     if (m==-1)
     {
              i=a[n-1];
              a=realloc(a,(n-1)*sizeof(int));
              n--;
              return i;
     }
     else
     {
         for (i=0;i<m;i++);
         j=a[i];
         for (;i<(n-1);i++) a[i]=a[i+1];
         a=realloc(a,(n-1)*sizeof(int));
         n--; 
         return j;  
     }    
}


int count(int m)
{
    int j=0;
    for(int i=0;i<n;i++) if(a[i]==m)j++;
    return j;
}
              
void reverse()
{int temp;
int i=0,j=n-1;
     for (;i<(n/2);i++,j--)
     {
         temp =a[i];
         a[i]=a[j];
         a[j]=temp;
     }
}

void sort()
{int temp;
     for (int i=0;i<n;i++)
     {
         for (int j=0;j<n;j++)
         {
             if (a[j]>a[j+1])
             {
                             temp=a[j];
                             a[j]=a[j+1];
                             a[j+1]=temp;
             }
         }
     }
}


void remov(int m)
{
     int j,k;
     
     j=index1(m);
     if (j==-1)
     {
               printf("NO SUCH ELEMENT");
     }
     else
     { 
         for (k=0;k<j;k++);
         for (;k<n;k++) a[k]=a[k+1];
         a=realloc(a,(n-1)*sizeof(int));
         n--;
     }
}

     
         

	
int main()
{
int ch=0,m,n;
while (ch!=10)
{
	printf("\n\t\t\tMENU\n1. Append\n2. Insert\n3. Index\n4. Merge two lists\n5. Sort\n6. Count\n7. Remove\n8. Pop\n9. Reverse\n10. Exit\nEnter your choice ");
	scanf("%d",&ch);
	switch(ch)
	{
		case(1): printf("\nEnter the element: ");
			scanf("%d",&m);
			append(m);
			print(); break;
		case(2): printf("\nEnter the position you want to insert the element: ");
			scanf("%d",&n);
			printf("\nEnter the element: ");
			scanf("%d",&m);
			insert(m,n);
			print(); break;
		case(3): printf("\nEnter the element you want to search: ");
			scanf("%d",&m);
			n=index1(m);
			printf("\n The element is found at position %d.\n",n);
			print(); break;
		case(4):merge();
			print(); break;
		case(5):sort();
			print(); break;
		case(6):printf("\nEnter the element you want to count: ");
			scanf("%d",&m);
			n=count(m);
			printf("\nNumber of occurances is %d\n",n);
			print(); break;
		case(7):printf("\nEnter the element you want to remove");
			scanf("%d",&m);
			remov(m);
			print(); break;
		case(8):printf("\nEnter the position of element you want to pop (-1 for the last element)");
			scanf("%d",&m);
			n=pop(m);
			printf("\nThe removed element is %d\n",n);
			print(); break;
		case(9):reverse();
			print(); break;
		case(10):return 0;
		default:printf("\nWrong choice!");
	}
}

}

